package just.stuff.morsels.ui.label;

import java.util.ArrayList;
import java.util.List;

import just.stuff.morsels.MorselApplication;
import just.stuff.morsels.objects.Morsel;
import just.stuff.morsels.util.TextLink;

import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;

public class LabelList extends Panel {

	public LabelList(String id, IModel<Morsel> mainModel ) {
		super( id, mainModel );
		IModel<List<String>> model = new AbstractReadOnlyModel<List<String>>() {
			@Override
			public List<String> getObject() {
				Morsel morsel = (Morsel) LabelList.this.getDefaultModelObject();
				if( morsel.isRoot() ) {
					return MorselApplication.dataSource().labels();
				} else {
					return new ArrayList<String>(morsel.getLabels());
				}
			}
		};
		add( new ListView<String>( "labels", model) {
			@Override
			protected void populateItem(final ListItem<String> item) {
				TextLink<Void> link = new TextLink<Void>( "link", LabelPage.class, item.getModel() );
				link.setParameter( "label", item.getModelObject() );
				item.add( link );
			}
		});
		
	}
	
}
